package zozopo.manual.image;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ScaleFilter {
	private int width;
	private int height;

    /**
     * Construct a ScaleFilter.
     */
	public ScaleFilter() {
		this(32, 32);
	}

    /**
     * Construct a ScaleFilter.
     * @param width the width to scale to
     * @param height the height to scale to
     */
	public ScaleFilter( int width, int height ) {
		this.width = width;
		this.height = height;
	}

    public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
		if ( dst == null ) {
			ColorModel dstCM = src.getColorModel();			
			dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster( width, height ), 
					dstCM.isAlphaPremultiplied(), null);
		}

		Image scaleImage = src.getScaledInstance( width, height, Image.SCALE_SMOOTH);
		Graphics2D g = dst.createGraphics();
		g.drawImage( scaleImage, 0, 0, width, height, null );
		g.dispose();

        return dst;
    }

	public String toString() {
		return "Distort/Scale";
	} 
	
	
	public static void main(String[] args) throws IOException{
		ScaleFilter sf = new ScaleFilter(192,289);
		File imageFile = new File("D:\\apache-tomcat-7.0.28\\webapps\\zozopo\\manual\\content\\2012\\12\\images\\1349423367385121.jpg");
		File dstImageFile = new File("D:\\apache-tomcat-7.0.28\\webapps\\zozopo\\manual\\content\\2012\\12\\images\\small\\1349423367385121_s.jpg");
		BufferedImage src = ImageIO.read(imageFile);
	//	BufferedImage dst = ImageIO.read(dstImageFile);
		
		
		
		ImageIO.write(sf.filter(src, null),"jpg",dstImageFile);
	}

}
